package com.cpr.demo.service.dsctl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cpr.demo.dto.CprUserDto;
import com.cpr.demo.model.CprUser;
import com.cpr.demo.repository.dsctl.UserMapper;
import com.cpr.demo.service.CprUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author Chen Panrui
 * created at 2022/3/18 22:02
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class CprUserServiceImpl implements CprUserService {

    private final UserMapper userMapper;

    @Override
    public Optional<CprUserDto> findOne(Long id) {
        CprUser model = userMapper.selectById(id);
        if(model == null){
            return Optional.empty();
        }
        return Optional.of(model.buildDto());
    }

    @Override
    public Optional<List<CprUserDto>> findList(CprUserDto param) {
        QueryWrapper<CprUser> query = new QueryWrapper<>();
        query.lt("id",param.getMaxId());
        List<CprUser> list = userMapper.selectList(query);
        if(CollectionUtil.isEmpty(list)){
            return Optional.empty();
        }
        return Optional.of(list.stream().map(CprUser::buildDto).collect(Collectors.toList()));
    }
}
